<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class WxuserMcard extends Model
{
    public $table = 'wxuser_mcard';

    public $primaryKey = 'id_wxuser_mcard';

    public $guarded = [];

    public $timestamps = false;

    public function getCardShadingAttribute($value)
    {
        return env('CARD_SHADING_URL').$value;
    }


    public function wxcardPrerogative()
    {
        return $this->hasMany(WxcardPrerogative::class,'id_mcard','id_mcard')->where('is_del','N');
    }

    public function getJbCardLevel($my_level)
    {
        if(!in_array($my_level,[1,2,3])) $my_level=1;
        $relation[1]=1002;
        $relation[2]=1008;
        $relation[3]=1001;

        return $relation[$my_level];
    }

    public function creditsRecords()
    {
        return $this->hasMany(CreditsRecod::class,'id_wxuser_mcard','id_wxuser_mcard');
    }

    public function balanceRecords()
    {
        return $this->hasMany(BalanceRecord::class,'id_wxuser_mcard','id_wxuser_mcard');
    }

    public function jbWxuserCard()
    {
        return $this->hasMany(JbUserMcard::class,'cmmobile1','member_optional_telephone');
    }

    public function giftCardsBuy()
    {
        return $this->hasMany(GiftBuyUsed::class,'buy_id_wxuser_mcard','id_wxuser_mcard')->where('status','Y');
    }

    public function giftCardBind()
    {
        return $this->hasMany(GiftBuyUsed::class,'use_id_wxuser_mcard','id_wxuser_mcard')->where('status','Y');
    }

    public function exchangeRecord()
    {
        return $this->hasMany(CreditsExchangeRecord::class,'id_wxuser_mcard','id_wxuser_mcard');
    }

    public function wxcardTickets()
    {
        return $this->hasMany(WxcardTicket::class,'id_wxuser_mcard','id_wxuser_mcard')->orderBy('created_at','desc');
    }

    public function mcard()
    {
        return $this->hasOne(Mcard::class,'id_mcard','id_mcard')->select(['id_mcard','card_bgcolor','card_shading','name']);
    }

    public function jbUserMcard()
    {
        return $this->hasMany(JbUserMcard::class,'cmtel','member_optional_telephone');
    }

    public function saleInfos()
    {
        return $this->hasMany(SaleInfo::class,'custno','card_no')->orderBy('saledate','desc');
    }

    public function merchant()
    {
        return $this->hasOne(Merchant::class,'id_merchant','id_merchant');
    }
}
